import {count} from '@/common/utils/count.js'
//常用的工具方法类
export const tools = {
	//CHN=miguan&BT=1&DN=MG03001002类型的参数转对象
	 paramsToObj(params) {
		var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
		var obj = {};
		while (reg.exec(params)) {
			obj[RegExp.$1] = RegExp.$2;
		}
		return obj;
	},
	//https://ky.railshj.cn?CHN=miguan&BT=1&DN=MG03001002带url的类型的参数转对象
	urlParmasToObj(url) {
		var reg_url = /^[^\?]+\?([\w\W]+)$/,
				reg_para = /([^&=]+)=([\w\W]*?)(&|$)/g, //g is very important
				arr_url = reg_url.exec(url),
				ret = {};
		if (arr_url && arr_url[1]) {
			var str_para = arr_url[1], result;
			while ((result = reg_para.exec(str_para)) != null) {
				ret[result[1]] = result[2];
			}
		}
		return ret;
	},
	getAstro(m, d) {
		return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m * 2 - (d < "102223444433".charAt(m - 1) - -19) * 2, 2) + '座'
	},
	getAge(strAge) {
		var birArr = strAge.split("-");
		var birYear = birArr[0];
		var birMonth = birArr[1];
		var birDay = birArr[2];
	
		d = new Date();
		var nowYear = d.getFullYear();
		var nowMonth = d.getMonth() + 1; //记得加1
		var nowDay = d.getDate();
		var returnAge;
	
		if (birArr == null) {
			return false
		};
		var d = new Date(birYear, birMonth - 1, birDay);
		if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
			if (nowYear == birYear) {
				returnAge = 0; // 
			} else {
				var ageDiff = nowYear - birYear; // 
				if (ageDiff > 0) {
					if (nowMonth == birMonth) {
						var dayDiff = nowDay - birDay; // 
						if (dayDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					} else {
						var monthDiff = nowMonth - birMonth; // 
						if (monthDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					}
				} else {
					return "出生日期晚于今天，数据有误"; //返回-1 表示出生日期输入错误 晚于今天
				}
			}
			return returnAge;
		} else {
			return ("输入的日期格式错误！");
		}
	},
	//把utf16的emoji表情字符进行转码成八进制的字符
	//上传emoji表情
	emojiToEntities(str) {
	    var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则  
	    return str.replace(patt, function (char) {
	      var H, L, code;
	      if (char.length === 2) {
	        H = char.charCodeAt(0); // 取出高位  
	        L = char.charCodeAt(1); // 取出低位  
	        code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法  
	        return "&#" + code + ";";
	      } else {
	        return char;
	      }
	    });
	},
	//将编码后的八进制的emoji表情重新解码成十六进制的表情字符
	//展示emoji表情
	entitiesToEmoji(str) {
	  return str.replace(/&#(\d+);/g, function (match, dec) {
	    let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
	    let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
	    return String.fromCharCode(H, L);
	   });
	},
	getSpecs(specList){
		if(!specList) return ''
		//将选中的spec以,隔开
		var specs_sel = ''
		for(var item of JSON.parse(specList)){
			specs_sel = specs_sel + item.name + '，'
		}
		specs_sel = specs_sel.substring(0, specs_sel.length-1)
		return specs_sel
	}
}